package com.example.overdue;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Spinner;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.fresh.R;
import com.example.overdue.adapter.MyRecycleViewAdapter;
import com.example.overdue.util.DataUtil;


public class Fragment_overdue extends Fragment {
    public Fragment_overdue() {

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        View view = inflater.inflate(R.layout.activity_main1, container, false);
        return view;
    }
    @Override
    public void onStart() {
        super.onStart();
        //添加监听器，为年份下拉列表设置事件的响应
        ((Spinner) getView().findViewById(R.id.spinner_year)).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                //拿到被选择项的值
                String str = (String) ((Spinner)getView().findViewById(R.id.spinner_year)).getSelectedItem();
                //修改年份的值
                setYear(str);
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
            }
        });

        ((Spinner)getView().findViewById(R.id.spinner_year)).setSelection(0);

    }

    private void setYear(String year) {
        //获取列表控件
        RecyclerView rvShow  = getView().findViewById(R.id.rv_show);
        // 设置布局
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        rvShow.setLayoutManager(linearLayoutManager);
        //为列表控件设置适配器
        rvShow.setAdapter(new MyRecycleViewAdapter(DataUtil.getOberdueList(),year));

    }

}
